package com.yunxi.service.localService.impl.sat;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.yunxi.core.page.PageHelperPlus;
import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.dao.sat.SatTargetMapper;
import com.yunxi.model.sat.SatTarget;
import com.yunxi.sat.pojo.SatTargetVO;
import com.yunxi.service.localService.sat.SatTargetService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2019-07-24
 */
@Service
public class SatTargetServiceImpl extends ServiceImplPlus<SatTargetMapper, SatTarget> implements SatTargetService {

    @Override
    public List listByPartner(Map map) {
        return baseMapper.listByPartner(map);
    }

    @Override
    public void saveByPartner(List<SatTargetVO> satTargetVOList, LoginInfo loginInfo) {
        if(satTargetVOList!=null&&satTargetVOList.size()>0){
            for (int i = 0; i < satTargetVOList.size(); i++) {
                SatTargetVO vo = satTargetVOList.get(i);

                //SatTargetVO temp = new SatTargetVO();
                //temp.setUserCode(vo.getUserCode());
                //temp.setYm(vo.getYm());
                //temp = (SatTargetVO)getByVo(temp);

                QueryWrapper qw = new QueryWrapper();
                qw.eq("USER_CODE", vo.getUserCode());
                qw.eq("YM", vo.getYm());
                List<SatTarget> satTargetList =    this.list(qw);
                SatTarget st = new SatTarget();
                BeanUtils.copyProperties(vo,st);
                st.setPartnerCode(loginInfo.getPartnerCode());//设置渠道商code
//                if(StringUtils.isEmpty(vo.getTargetId())){
                if(satTargetList.size()==0){
                    baseMapper.insert(st);
                }else{
                    st.setTargetId(satTargetList.get(0).getTargetId());
                    baseMapper.updateById(st);
                }
            }
        }
    }

    /**
     * 厂端目标分页
     * @param satTargetVO
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<SatTargetVO> factoryPage(SatTargetVO satTargetVO, Integer pageNum, Integer pageSize) throws Exception {
        PageHelperPlus.startPage(pageNum,pageSize);
        List<SatTargetVO> list = baseMapper.factoryPage(satTargetVO);
        PageInfo<SatTargetVO> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
